package Database;

import java.sql.Connection;
import java.sql.DriverManager;

import javax.naming.Context;
import javax.naming.InitialContext;

public class DBConnection
{
	private static String dbdriver = "com.mysql.jdbc.Driver";
	private static String username = "root";
	private static Connection con;

	public static Connection connect(boolean autoCommit)
	{
		try
		{
			Context env = (Context)new InitialContext().lookup("java:comp/env"); 
			//Get a single value String 
//			String password = (String)env.lookup("DBPassword"); 
			String password = "password";
			String url = (String) env.lookup("DBUrl");
			if (con==null || con.isClosed()) {
				Class.forName(dbdriver);
				String s = new StringBuffer().append(url).append("?user=").append(username).append("&password=").append(password).toString();
				System.out.println(s);
				con = DriverManager.getConnection(s);
				con.setAutoCommit(autoCommit);
			}
		}
		catch (Exception e)
		{
			System.err.println("Exception: " + e.getMessage());
		}

		return con;
	}
	
}
